var requestDelay = 2000;

// Init the form once the document is ready
$( init );

// Initialize the form

function init() {
	
  // Hide the form initially.
  // Make requestForm() the form's submit handler.
  // Position the form so it sits in the center of the browser window.
	$('#addItemForm').hide().submit( submitForm2 ).addClass( 'positioned' );
  $('#requestForm').hide().submit( submitForm ).addClass( 'positioned' );
  
  // 1. Fade the content out
  // 2. Display the form
  // 3. Move focus to the first field
  // 4. Prevent the link being followed
  $('.request-join').click( function() {
    $('.viewContent').fadeTo( 'slow', .3 );
    $('#requestForm').fadeIn( 'slow', function() {
      $('#request').focus();
    } )

    return false;
  } );
  $('.addItem').click( function() {
	    //$('#my-timeline').fadeTo( 'slow', .3 );
	    $('#addItemForm').fadeIn( 'slow', function() {
	      $('#itemDescription').focus();
	    } )

	    return false;
	  } );
  
  // When the "Cancel" button is clicked, close the form
  $('#cancel3').click( function() { 
  
    $('.viewContent').fadeTo( 'slow', 1 );
    $('#requestForm').fadeOut();
  } );
  $('#cancel6').click( function() { 
	  
	    $('.viewContent').fadeTo( 'slow', 1 );
	    $('#addItemForm').fadeOut();
	  } );
  
  // When the "Escape" key is pressed, close the form
  $('#requestForm').keydown( function( event ) {
    if ( event.which == 27 ) {
      
      $('.viewContent').fadeTo( 'slow', 1 );
      $('#requestForm').fadeOut();
    }
  } );
  $('#addItemForm').keydown( function( event ) {
	    if ( event.which == 27 ) {
	      
	      $('.viewContent').fadeTo( 'slow', 1 );
	      $('#addItemForm').fadeOut();
	    }
	  } );
  
}

  function submitForm() {
	  var requestForm = $(this);

	  // Are all the fields filled in?

	  if ( !$('#request').val()  ) {

	    // No; display a warning message and return to the form
	    $('#incompleteRequest').fadeIn().delay(requestDelay).fadeOut();
	    contactForm.fadeOut().delay(requestDelay).fadeIn();

	  } else {
	    // Yes; submit the form via Ajax
	    $('#sendingRequest').fadeIn();
	    requestForm.fadeOut();
	    $('.viewContent').fadeTo( 'slow', 1 );
	    $.post('/application/sendActionItemJoinRequest/'+document.myform1.request.value +'/'+ 
	    		document.getElementById("planID").value+'/'+document.getElementById("itemID").value, function(data){successRequest("S E N T successfully"); });
	  	 }
	  // Prevent the default form submission occurring
	  return false;
	}
  function submitForm2() {
	  var Item = $(this);

	  // Are all the fields filled in?

	  if ( !$('#itemDescription').val()  ) {

	    // No; display a warning message and return to the form
	    $('#emptyListItem"').fadeIn().delay(requestDelay).fadeOut();
	    Item.fadeOut().delay(requestDelay).fadeIn();
	    return false;
	  } else {
	    return true;

	  	 }
	}
  
  function successRequest( response ) {
	  response = $.trim( response );
	  $('#sendingRequest').delay(2000).fadeOut();
	  $('#'+document.getElementById("itemID").value).hide()
	  if ( response == "S E N T successfully" ) {

	    // Form sent successfully
	    	
	    // 1. Display the success message
	    // 2. Clear the form fields
	    // 3. Fade the content back in

	    $('#successRequest').fadeIn().delay(requestDelay).fadeOut();
	    $('#request').val( "" );

	  } else {

	    // Form submission failed: Display the failure message,
	    // then redisplay the form
	    $('#failureRequest').fadeIn().delay(requestDelay).fadeOut();
	    $('#requestForm').delay(requestDelay+500).fadeIn();
	  }
	  // clear the values
	
	}
  
  